﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Globalization;

namespace ClipboardCalc
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            calc();
        }



        private void calc()
        {
            decimal wert = 0, temp =0;
            string cb;

            if (System.Windows.Forms.Clipboard.ContainsText())
            {
                cb = System.Windows.Forms.Clipboard.GetText();

                using (StringReader reader = new StringReader(cb))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {

                        string[] Zeile = line.Split("\t".ToCharArray());
                        foreach (string st in Zeile)
                        {
                            try
                            {
                                temp = System.Convert.ToDecimal(st);
                            }
                            catch (FormatException)
                            {
                            }

                            wert += temp;
                        }
                    }

                    textBox2.Text = wert.ToString("N4", CultureInfo.InvariantCulture);
                    // Und wenn die Liste noch leer ist?
                    if(wert.ToString() != listBox1.Items[0].ToString())
                     listBox1.Items.Insert(0,wert.ToString());
                }
            }
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            calc();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ProcessStartInfo sInfo = new ProcessStartInfo(e.Link.LinkData.ToString());
            Process.Start(sInfo);

            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            linkLabel1.Links.Remove(linkLabel1.Links[0]);
            linkLabel1.Links.Add(0, linkLabel1.Text.Length, "http://saschalorenz.blogspot.com");
            listBox1.Items.Add("0");
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = (System.Convert.ToDecimal(textBox2.Text) -  System.Convert.ToDecimal(listBox1.SelectedItem.ToString())).ToString();
        }


    }
}
